using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using IGlobalist.Cms.Model;
using Ader.TemplateEngine;
using NVelocityTemplateEngine;
using NVelocityTemplateEngine.Interfaces;
using IGlobalist.Cms.Bll;




namespace IGlobalist.Cms.Presentation
{
    public class ViewController
    {
        private RenderContext _Context;
        private ITemplateEngine _Engine;

        public ITemplateEngine TemplateEngine
        {
            get { return _Engine; }
            set { _Engine = value; }
        }
        private System.Web.UI.Page _Container;
        public ViewController(BaseCmsPage container, long pageID, long categoryID, long contentID)
        {

                long pageLoginID = 0, categoryParent=0;
                string nameCategoryParent = "";
                _Container = container;
                _Context = new RenderContext();
                _Context.CurrentPage = PageController.GetPageCurrent(pageID);
                _Context.PageList = PageController.GetObjectListRelateToObjectID(_Context.CurrentPage.ID);

                _Context.CategoryList = _Context.CurrentPage.CategoryList;
                _Context.CurrentCategory = CategoryController.GetCategory(categoryID);
                _Context.CurrentContent = ContentController.GetContent(contentID);
                _Context.CategoryNode = CategoryController.GetCategoryNode(categoryID);

                try
                {
                    foreach (IPage p in _Context.PageList)
                    //    //login
                        if (p.Type ==  (short) PageType.Login)
                            pageLoginID = p.ID;
                }
                catch
                {
                }

                try
                {
                    if (categoryID == 0)
                    {
                        _Context.ContentList = ContentController.GetContentListLelateToObjectID(_Context.CurrentPage.ID, CmsObjectType.Page);
                    }
                    else
                    {
                        _Context.ContentList = ContentController.GetContentListLelateToObjectID(categoryID, CmsObjectType.ContentCategory);
                    }

                    if ((_Context.ContentList != null && _Context.ContentList.Count == 1) || contentID > 0)
                    {
                        if (contentID == 0) contentID = _Context.ContentList[0].ID;
                        _Context.CurrentContent = ContentController.GetContent(contentID);
                        _Context.NoteList = _Context.CurrentContent.NoteList;
                    }
                }
                catch { }

                try
                {
                    if (categoryID == 0)
                    {
                        _Context.ProductList = ProductController.GetProductListLelateToObjectID(_Context.CurrentPage.ID, CmsObjectType.Page);

                    }
                    else
                    {
                        _Context.ProductList = ProductController.GetProductListLelateToObjectID(categoryID, CmsObjectType.ContentCategory);

                    }

                    if ((_Context.ProductList != null && _Context.ProductList.Count == 1) || contentID > 0)
                    {
                        //if (contentID == 0) contentID = _Context.ProductList[0].ID;
                        if (contentID > 0)
                            _Context.CurrentProduct = ProductController.GetProduct(contentID);
                    }
                }
                catch { }

                foreach (IContentCategory c in _Context.CategoryList)
                {
                    foreach (IContentCategory item in c.CategoryList)
                        if (item.ID == categoryID)
                        {
                            categoryParent = c.ID;
                            nameCategoryParent = c.Name;
                            break;
                        }
                }
            

                _Context.AdContentList = AdController.GetAdListByPageID(_Context.CurrentPage.ID);

                try
                {
                    _Context.VideoList = VideoController.GetVideoList();
                    if (categoryID > 0)
                        _Context.VideoSubList = VideoController.GetVideoListByCategoryId(categoryID);
                    if (contentID > 0)
                        _Context.VideoCurrent = VideoController.GetVideo(contentID);
                    _Context.VideoViewList = VideoController.GetVideoViewList();
                    _Context.VideoRateList = VideoController.GetVideoRateList();
                }
                catch
                {
                }

                #region class

                long classId = 0, post=0;
                IList<ICmsClass> cList = ClassController.GetClassListByClassTypeID((short)CmsObjectType.Page, true);
                foreach (ICmsClass clazz in cList)
                    for (int i = 0; i < clazz.PropertyList.Count; i++)
                        if ((Convert.ToString(clazz.PropertyList[i].Name) == "TypeClass") && (Convert.ToString(clazz.PropertyList[i].Value) == "Forum"))
                        {
                            classId = clazz.ID;
                            break;
                        }

                PageList ForumList = new PageList();
                ForumList = PageController.GetPagesByClass(classId);
                _Context.ForumList = ForumList;
                
                foreach( IPage p in ForumList)
                {
                    post += p.ContentList.Count;
                }
               

                #endregion


                ITemplate template = TemplateController.GetTemplate(_Context.CurrentPage.TemplateID);
                _Engine = TemplateEngineFactory.GetTemplateEngine(template);
                _Engine.AddToContext("RenderCtx", _Context);
                _Engine.AddToContext("Container", _Container);
                _Engine.AddToContext("PageID", pageID);
                _Engine.AddToContext("ContentID", contentID);
                _Engine.AddToContext("CategoryParentID", categoryParent);
                _Engine.AddToContext("NameCategoryParentID", nameCategoryParent);
                _Engine.AddToContext("CategoryID", categoryID);
                _Engine.AddToContext("PageLoginID", pageLoginID);
                _Engine.AddToContext("SiteAccessNumber", System.Web.HttpContext.Current.Session["siteAccessNumber"]);
                _Engine.AddToContext("Post", post);

        }
        public void Render()
        {
            _Engine.Render(_Container.Response.Output);
        }
    }  
}
